Strings
1. len()
function
The len()
function returns the number of characters in a string.
text = "Hello, World!"
print(len(text)) # Output: 13
2. Accessing String Characters
1. String Indexing
Accessing individual characters in a string using their position (index).
text = "Python"
print(text[0]) # Output: P
print(text[-1]) # Output: n (negative indexing starts from the end)
2. String Looping
Iterating through each character in a string.
text = "Python"
# more commonly used is this shorthand
for char in text:
print(char)
# using the range() and len() function
for char in range(len(text)):
print(char)
3. String Concatenation
Combining two or more strings into a single string using the +
operator.
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
4. String Slicing
Extracting a portion of a string using a start and end index. String slicing returns the sliced part as a new string, not making changes to actual string.
- The starting index is inclusive, and the ending index is exclusive.
# syntax
my_string[start:end]
text = "Python Programming"
print(text[0:6]) # Output: Python
With string slicing we are actually not required to specify either the start or the end index.
- If we don't specify the start, it's equivalent to starting from the beginning of the string.
- If we don't specify the end, it's equivalent to ending at the end of the string.
- If we don't specify either, we get the entire string.
text = "Python Programming"
print(text[:3]) # Output: Pyt
print(text[3:]) # Output: hon Programming
print(text[:]) # Output: Python Programming
Advanced slicing techniques, including step size.
# syntax
my_string[start:end:step]
text = "Python Programming"
print(text[::2]) # Output: Pto rgamn (every second character)
5. Reverse a String
We can also use slicing to reverse a string. By not specifying the starting index or the ending index, and setting the step to -1, the string will be reversed.
my_string = "Hello"
print(my_string[::-1]) # Output: olleH
6. String are Immutable
In Python, strings are immutable, which means they cannot be changed after they are created. It's important to know that whenever you slice a string, you are not modifying the underlying string. Instead, you are creating a new string with the sliced characters.
message = "I will never change."
message[0] = "X" # This will cause an error
message = "I will never change."
before_second = message[:1] # "I"
after_second = message[2:] # "will never change."
new_message = before_second + after_second
7. String Formatting
1. format()
method
We can format strings in Python using the .format()
method.
- We have a string with two placeholders:
{}
. - We then call the
format
method on the string and pass in the values we want to replace the placeholders with. - The values are passed in the order they are to be inserted.
- The number of placeholders must match the number of arguments passed to the
format
method.
name = "Alice"
age = 25
msg = "Hello, {}. You are {} years old.".format(name, age)
print(msg) # Output: Hello, Alice. You are 25 years old.
You can also use the index of the placeholders to specify the order of the arguments.
name = "Alice"
age = 25
msg = "Hello, {1}. You are {0} years old.".format(age, name)
print(msg) # Output: Hello, Alice. You are 25 years old.
2. f-strings
An even more concise way to format strings is to use f-strings. These are prefixed with an f
before the string and allow you to insert variables/expressions/function calls into the string.
name = "Alice"
age = 25
msg = f"Hello, {name}. You are {age} years old."
print(msg) # Output: Hello, Alice. You are 25 years old.